package controller

import (
	"gitee.com/Frank098/devops-app-mgt/model"
	"gitee.com/Frank098/devops-app-mgt/service"
	"github.com/gin-gonic/gin"
)

var Deployment deployment

type deployment struct{}

// @Tags Deployment
// @Summary 获取Deployment详细信息
// @Accept json
// @Produce json
// @Param deployment_name query string true "deployment名称"
// @Param namespace query string true "deployment命名空间"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/info [GET]
func (d *deployment) GetDeploymentInfo(ctx *gin.Context) {
	var params model.GetDeploymentInfo
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Deployment.GetDeploymentInfo(params.DeploymentName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Deployment
// @Summary 修改Deployment副本数
// @Accept json
// @Produce json
// @Param "" body model.ScaleDeployment true "修改Deployment副本数"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/scale [PUT]
func (d *deployment) ScaleDeployment(ctx *gin.Context) {
	var params model.ScaleDeployment
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Deployment.ScaleDeployment(params.DeploymentName, params.Namespace, params.ScaleNum)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Deployment
// @Summary 删除Deployment
// @Accept json
// @Produce json
// @Param "" body model.GetDeploymentInfo true "删除Deployment"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/delete [DELETE]
func (d *deployment) DeleteDeployment(ctx *gin.Context) {
	var params model.GetDeploymentInfo
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Deployment.DeleteDeployment(params.DeploymentName, params.Namespace); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "删除Deployment成功",
	})
}

// @Tags Deployment
// @Summary 重新编排Deployment
// @Accept json
// @Produce json
// @Param "" body model.GetDeploymentInfo true "重新编排Deployment"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/restart [PUT]
func (d *deployment) RestartDeployment(ctx *gin.Context) {
	var params model.GetDeploymentInfo
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Deployment.RestartDeployment(params.DeploymentName, params.Namespace); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "重新编排Deployment成功",
	})
}

// @Tags Deployment
// @Summary 获取每个namespace的deployment资源
// @Accept json
// @Produce json
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/all [GET]
func (d *deployment) GetDeployNumPerNp(ctx *gin.Context) {
	data, err := service.Deployment.GetDeployNumPerNp()
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Deployment
// @Summary 获取指定特殊部分namespace的deployment资源
// @Accept json
// @Produce json
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/Special [GET]
func (d *deployment) GetDeploySpecial(ctx *gin.Context) {
	data, err := service.Deployment.GetDeploySpecial()
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Deployment
// @Summary 获取指定namespace的deployment资源
// @Accept json
// @Produce json
// @Param namespace query string true "deployment命名空间"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/single [GET]
func (d *deployment) GetDeploySingleNp(ctx *gin.Context) {
	var params model.GetDeploymentInfo
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Deployment.GetDeploySingleNp(params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Deployment
// @Summary 创建Deployment
// @Accept json
// @Produce json
// @Param "" body model.GetDeploymentInfo true "创建Deployment"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/create [POST]
func (d *deployment) CreateDeployment(ctx *gin.Context) {
	var params model.DeployCreate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Deployment.CreateDeployment(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "创建Deployment成功",
	})
}

// @Tags Deployment
// @Summary 获取Deployment资源编排文件
// @Accept json
// @Produce json
// @Param deployment_name query string true "deployment名称"
// @Param namespace query string true "deployment命名空间"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /deployments/yaml [GET]
func (d *deployment) GetDeployYaml(ctx *gin.Context) {
	//
	var params model.GetDeploymentInfo
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Deployment.GetDeployYaml(params.DeploymentName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

/*
// 更新deployment
func (d *deployment) UpdateDeployment(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	err := service.Deployment.UpdateDeployment(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新Deployment成功",
		"data": nil,
	})
}

*/
